var default_communication = $("#default_communication").attr("value");			
if(default_communication=="0"){
	default_communication='1';//QQ
}

$(document).ready(
	   function()
	   {
			  if(default_communication=='1'){
					$("#QQ_NUMBER").css('display','inline-block');
//					if($.trim($("#qq").val()).length!=0){
//							$("#qq").attr("readonly",true);
//					}
//					if($.trim($("#phone").val()).length!=0){
//						$("#phone").attr("readonly",true);
//					}
					$("#SKYPE_NUMBER").css('display','none');
					$("#PHONE_NUMBER").css('display','inline-block');
					$("#ENGLISH_NAME").css('display','inline-block');
			  }

			  if(default_communication=='2'){
//				 if($.trim($("#skype_id").val()).length!=0){
//				    $("#skype_id").attr("readonly",true);
//				 }
//				 if($.trim($("#phone").val()).length!=0){
//				    $("#phone").attr("readonly",true);
//				 }
				$("#QQ_NUMBER").css('display','none');
				$("#SKYPE_NUMBER").css('display','inline-block');
				$("#PHONE_NUMBER").css('display','inline-block');
				$("#ENGLISH_NAME").css('display','inline-block');
			 }

			if(default_communication=='3'){
				$("#QQ_NUMBER").css('display','none');
				$("#SKYPE_NUMBER").css('display','none');
				$("#PHONE_NUMBER").css('display','inline-block');
				$("#ENGLISH_NAME").css('display','inline-block');
				if($.trim($("#phone").val()).length!=""){
					    $("#phone").attr("readonly",true);
				 }
			}			
	   }
);
		

$("#communication_tools").change(function(){
	var selected_tool = $(this).attr("value");
	var old_qq = $("#old_qq").attr("value");
	var old_skype_id = $("#old_skype").attr("value");
	if(selected_tool=='1'){
			$("#QQ_NUMBER").css('display','inline-block');
			$("#SKYPE_NUMBER").css('display','none');
			$("#PHONE_NUMBER").css('display','inline-block');
			$("#ENGLISH_NAME").css('display','inline-block');
//			if($.trim($("#old_qq").val()).length!=0){
//				$("#qq").attr("readonly",true);
//			}
//			if($.trim($("#old_phone").val()).length!=0){
//				$("#phone").attr("readonly",true);
//			}			
	}
	
	if(selected_tool=='2'){
			$("#QQ_NUMBER").css('display','none');
			$("#SKYPE_NUMBER").css('display','inline-block');
			$("#PHONE_NUMBER").css('display','inline-block');
			$("#ENGLISH_NAME").css('display','inline-block');
//			 if($.trim($("#old_skype").val()).length!=0){
//				    $("#skype_id").attr("readonly",true);
//			 }
//			 if($.trim($("#old_phone").val()).length!=0){
//			    $("#phone").attr("readonly",true);
//			 }			
	}
	
	if(selected_tool=='3'){
		$("#QQ_NUMBER").css('display','none');
		$("#SKYPE_NUMBER").css('display','none');
		$("#PHONE_NUMBER").css('display','inline-block');
		$("#ENGLISH_NAME").css('display','inline-block');
//		 if($.trim($("#old_phone").val()).length!=0){
//			    $("#phone").attr("readonly",true);
//		 }		
	}
})	

$("#userClassTypes").change(function(){
	var selected_value = $(this).attr("value");
	var today_monthly_booking = $("#today_monthly_booking").attr("value");
	if(selected_value=='user_monthly_class'){
		if(today_monthly_booking=="7"){
			alert("友情提示:您好,您预约的课程为包月课程,您已预订过您所选择日期的当天包月课程,谢谢!");
			$(this).attr("value","user_class");
		}
		
		if(today_monthly_booking=="22"){
			alert("友情提示:您好,您预约的课程为包月课程,需上完已预约的课程才能继续预约,谢谢!");
			$(this).attr("value","user_class");
		}
		
		if(today_monthly_booking=="23"){
			alert("友情提示:次日包月课程已预订,谢谢!");
			$(this).attr("value","user_class");
		}

		if(today_monthly_booking=="24"){
			alert("友情提示:今日不能预约明天以后的包月课程,谢谢！");
			$(this).attr("value","user_class");
		}
	}else{
		$("#p_user_class_type_error").html('')
	}
})

var user_info = new Object();
var realphone=1;
user_info.response = function(res){
	realphone = res.real_phone;
}

var user_phone_info = new Object();
var dumplicatePhone = 1;
user_phone_info.response = function(res){
	dumplicatePhone = 1;
	if(res.success=='fail_duplicate'){
		dumplicatePhone = 2;
		alert('该手机号已经为他人上课使用,请更换手机号,谢谢！')
	}
}

$("#confirm_book").click(function(){
	var first_checked =false;
	var first_value=0;
	
	$("input[name='course_type_radio']").each(  
		    function(){  
		    	var checked = $(this).attr("checked");
		    	if(checked=='checked'){
		    		first_checked= true;
		    		first_value =$(this).attr("value");
		    	}
		    }   
	)
	
	if(!first_checked){
		alert('请选择基本的学习教材分类');
		return;
	}
	
   if(first_value!=18){//自由会话不需要课程
			var second_exist = false;
			var second_checked = false;
			if($("input[name='course']").length>1){//因为点击预览时会有1个
				var second_exist = true;
				$("input[name='course']").each(  
						function(){  
							var checked = $(this).attr("checked");
							if(checked=='checked'){
								second_checked= true;
							}
						}   
				)
			}

			if(second_exist){
				if(!second_checked){
					alert('请选择基本的学习教材');
					return;
				}
			}

			var third_exist = false;
			if($("#thirdcourse")){
				var third_selected = false;
				var third_exist = true;
				var third_selected_value = $("#thirdcourse").attr("value");
			}
			
			if(third_exist){
				if(third_selected_value=='-1'){
					alert('请选择当节课的学习课程');
					return;
				}
			}
	}
	
	var communication_tools = $("#communication_tools").val();
	var qq = $("#qq").val();
	var skype = $("#skype_id").val();
	if(communication_tools==1){
		if($.trim(qq)==""){
			$("#qq").attr("value","");
			alert('请输入QQ号码');
			return;
		}
	}
	if(communication_tools==2){
		if($.trim(skype)==""){
			$("#skype").attr("value","");
			alert('请输入SKYPE昵称');
			return;
		}
	}
	
	var phone = $("#phone").val();
	$.ajax({
		 url:"user_getUserInfo.html",
		 data:"phone="+phone,
		 dataType: 'json',
		 success:user_info.response,
		 async:false
	});

	var phone = $("#phone").val();
	if($.trim(phone)==""){
		$("#phone").attr("value","");
		alert('请输入手机号码');	
		return;
	}
	
	$.ajax({
		 url:"user_verifyDuplicatePhone.html",
		 data:"phone="+phone,
		 dataType: 'json',
		 success:user_phone_info.response,
		 async:false
	});
	
	if(dumplicatePhone!=1){
		return;
	}
	if(realphone==1){
		alert('请验证手机号');
		return;
	}
	var english_name = $("#english_name").val();
	if($.trim(english_name)==""){
		$("#english_name").attr("value","");
		alert('请填写英文名称,以便老师与你交流');
		return;
	}
	document.Form1.submit();
})

window.returnValue="";
var updateBooking_info =new Object();
updateBooking_info.response = function(res){
	if(res.success=='success'){
		window.returnValue = res.course_chapter_name;
	}
	alert('更新成功');
	window.close();
}
$("#confirm_update").click(function(){
	var first_checked =false;
	var first_value=0;
	var second_value = 0;
	$("input[name='course_type_radio']").each(  
		    function(){  
		    	var checked = $(this).attr("checked");
		    	if(checked=='checked'){
		    		first_checked= true;
		    		first_value =$(this).attr("value");
		    	}
		    }   
	)
	
	if(!first_checked){
		alert('请选择基本的学习教材分类');
		return;
	}
	
	if(first_value!=18){//自由会话不需要课程
			var second_exist = false;
			if($("input[name='course']").length>0){
				var second_checked = false;
				var second_exist = true;
				$("input[name='course']").each(  
						function(){  
							var checked = $(this).attr("checked");
							if(checked=='checked'){
								second_checked= true;
								second_value = $(this).attr("value");
							}
						}   
				)
			}
			if(second_exist){
				if(!second_checked){
					alert('请选择基本的学习教材');
					return;
				}
			}
			var third_exist = false;
			if($("#thirdcourse")){
				var third_selected = false;
				var third_exist = true;
				var third_selected_value = $("#thirdcourse").attr("value");
			}
			
			if(third_exist){
				if(third_selected_value=='-1'){
					alert('请选择当节课的学习课程');
					return;
				}
			}
	}
	var id =$("#id").val();
	
	var postJsondata ={"course_radio_type":first_value,"course_id":second_value,"chapter_id":third_selected_value,"id":id};
	$.ajax({
		 type:'POST',
		 url:'user_subUpdateBooking.html',
		 data:postJsondata,
		 dataType: 'json',
		 success:updateBooking_info.response,
		 async:false
	});	
})		

$("#phone").change(function(){
	var phone = $("#phone").val();
	if($.trim(phone)==""){
		$("#phone").attr("value","");
		alert("请输入手机号码");
		return;
	}
	$.ajax({
		 url:"user_verifyDuplicatePhone.html",
		 data:"phone="+phone,
		 dataType: 'json',
		 success:user_phone_info.response,
		 async:false
	});
})	
